package com.fs.cv.controller;

import com.fs.cv.dao.RoleDao;
import com.fs.cv.pojo.pojodao.Permission;
import com.fs.cv.pojo.pojodao.Record;
import com.fs.cv.pojo.pojodao.Role;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.service.RoleService;
import com.fs.cv.service.TeachingService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @作者: zh
 * @时间: 2020/10/1 15:30
 * @描述: 教学相关控制器
 */
@RequestMapping("education/")
@Controller
public class EducationController {
    @Autowired
    private TeachingService teachingService;

    @Autowired
    private RoleService roleService;

    /**
     * 在获取课表之前
     * @return 视图所在
     */
    @RequestMapping("teaching/beforeGet")
    public String beginGet(Model model){
        Session session= SecurityUtils.getSubject().getSession();
        List<String> roles=new ArrayList<>();
        Set<String> permissions=new HashSet<>();
        User user= (User) session.getAttribute("user");
        for(Role r:user.getRoles()){
            roles.add(r.getName());
            Role role1=roleService.queryRoleInfoByRoleId(r.getRoleId());
            if (role1.getPermissions()!=null) {
                for (Permission per : role1.getPermissions()) {
                    permissions.add(per.getName());
                }
            }
        }
        session.setAttribute("roles",roles);
        session.setAttribute("permissions",permissions);
        model.addAttribute("dateStr",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return "teaching/tabs";
    }
    /**
     * 获取课表
     * @param date 要查询的日期
     * @param model 模型
     * @return 课表的视图
     */
    @RequestMapping("teaching/getTimetable")
    public String getTimeTable(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date,String aClass,HttpServletRequest request,Model model){
        int op=Integer.parseInt(request.getParameter("op"));
        //全部班级管理没有选择班级
        System.out.println(aClass);
        System.out.println(op);
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        model.addAttribute("dateStr", dateStr);
        if(aClass==null&&op==0)return "teaching/tab0-fail";
        else {
            //要传递的参数准备
            String[] dates = new String[7];
            if (date == null) date = new Date();
            Map<String,String> who=new HashMap<>();
            Record[][] records = teachingService.getRecords(date, aClass, op, dates,who);
            //向视图传递数据
            model.addAttribute("dates", dates);
            model.addAttribute("classStr", aClass);
            model.addAttribute("records", records);
            model.addAttribute("who",who.get("who"));
            model.addAttribute("op",op);
            //传递视图
            return "teaching/timetable";
        }
    }

    /**
     * ajax获得记录
     * @param id 要获得记录的id
     * @return 状态码及其数据
     */
    @RequestMapping("teaching/getRecord")
    @ResponseBody
    public Map<String,Object> getRecord(Integer id){
        Map<String,Object> result=new HashMap<>();
        Record record=teachingService.getRecord(id);
        if(record!=null){
            result.put("code",0);
            result.put("record",record);
        }else result.put("code",-1);
        return result;
    }

    /**
     * ajax修改记录
     * @param record 记录
     * @param request 请求，用来获取_class
     * @return 状态码
     */
    @RequestMapping("teaching/modifyRecord")
    @ResponseBody
    public Map<String,Object> modifyRecord(Record record, HttpServletRequest request){
        record.set_class(request.getParameter("_class"));
        Map<String,Object> result=new HashMap<>();
        boolean flag=teachingService.modifyRecord(record);
        if(flag){
            result.put("code",0);
            result.put("msg","修改成功");
        }else{
            result.put("code",-1);
            result.put("msg","修改失败");
        }
        return result;
    }

    /**
     * ajax移除记录
     * @param id 记录id
     * @return 结果
     */
    @RequestMapping("teaching/removeRecord")
    @ResponseBody
    public Map<String,Object> removeRecord(Integer id){
        Map<String,Object> result=new HashMap<>();
        Map<String,Object> map=new HashMap<>();
        boolean flag=teachingService.removeRecord(id,map);
        if(flag){
            result.put("code",0);
            result.put("msg","移除成功");
            result.put("i",map.get("i"));
            result.put("j",map.get("j"));
        }else{
            result.put("code",-1);
            result.put("msg","移除失败");
        }
        return result;
    }

    /**
     * ajax添加记录
     * @param record 记录
     * @param request 请求，用来获取_class
     * @return 结果
     */
    @RequestMapping("teaching/addRecord")
    @ResponseBody
    public Map<String,Object> addRecord(Record record, HttpServletRequest request){
        record.set_class(request.getParameter("_class"));
        Map<String,Object> result=new HashMap<>();
        System.out.println(record);
        boolean flag=teachingService.addRecord(record);
        System.out.println(record.getId());
        if(flag){
            result.put("code",0);
            result.put("msg","添加成功");
            result.put("id",record.getId());
        }else{
            result.put("code",-1);
            result.put("msg","添加失败");
        }
        return result;
    }

    /**
     * ajax调整记录(修改时间）
     * @param record 记录
     * @return 结果
     */
    @RequestMapping("teaching/adjustRecord")
    @ResponseBody
    public Map<String,Object> addRecord(Record record){
        Map<String,Object> result=new HashMap<>();
        System.out.println(record);
        Map<String,Object> map=new HashMap<>();
        boolean flag=teachingService.adjustRecord(record,map);
        if(flag){
            result.put("code",0);
            result.put("msg","调整成功");
            result.put("record",record);
            result.put("i",map.get("i"));
            result.put("j",map.get("j"));
        }else{
            result.put("code",-1);
            result.put("msg","调整失败");
        }
        return result;
    }

    /**
     * ajax为前端输入框提示信息获取数据
     * @param keyI 标识要查哪张表
     * @param keyWord 关键字
     * @return 返回前端结果
     */
    @RequestMapping("teaching/searchKeyWord")
    @ResponseBody
    public Map<String,Object> searchKeyWord(String keyI,String keyWord){
        Map<String,Object> result=new HashMap<>();
        System.out.println("keyI:"+keyI+" keyWord:"+keyWord);
        List<Map<String, Object>> maps = teachingService.searchBlurry(keyI, keyWord);
        System.out.println(maps);
        result.put("code",0);
        result.put("value",maps);
        return result;
    }
}
